package router

import (
	"strconv"
	"tushare/model"
	"tushare/model/tushare"
	serviceTushare "tushare/service/tushare"

	"github.com/gin-gonic/gin"
)

func (rt *Router) tushare() {
	tushareGroup := rt.R.Group("/tushare/")
	{
		tushareGroup.POST("updateDayJetton", func(c *gin.Context) {
			tsCode := c.PostForm("tsCode")
			dayJetton, _ := strconv.ParseFloat(c.PostForm("dayJetton"), 64)
			ts := model.TushareStock{}
			ts.DayJetton = dayJetton
			model.StockUpdate(tsCode, ts)
			c.JSON(200, gin.H{"code": 0, "message": "成功"})
		})

		tushareGroup.GET("stockList", func(c *gin.Context) {
			mapType := c.Query("type")
			page, _ := strconv.ParseInt(c.Query("page"), 10, 64)
			pageSize, _ := strconv.ParseInt(c.Query("pageSize"), 10, 64)
			tsCode := c.Query("tsCode")
			name := c.Query("name")
			closeAmount, _ := strconv.ParseFloat(c.Query("close"), 64)
			collect, _ := strconv.ParseInt(c.Query("collect"), 10, 64)
			data := model.StockListPage(page, pageSize, tsCode, collect, mapType, closeAmount, name)
			count := model.StockCount(tsCode, collect, mapType, closeAmount, name)
			c.JSON(200, gin.H{"code": 0, "message": "股票列表", "data": data, "count": count})
		})

		tushareGroup.POST("moneyFlow", func(c *gin.Context) {
			tsCode := c.PostForm("tsCode")
			page, _ := strconv.ParseInt(c.PostForm("page"), 10, 64)
			pageSize, _ := strconv.ParseInt(c.PostForm("pageSize"), 10, 64)
			data := tushare.MoneyFlowList(page, pageSize, tsCode)
			count := tushare.MoneyFlowCount(tsCode)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data, "count": count})
		})

		//资金流入
		tushareGroup.POST("capitalInflow", func(c *gin.Context) {
			date := c.PostForm("date")
			data := tushare.CapitalInflow(date)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data})
		})

		tushareGroup.POST("referenceSubstanceList", func(c *gin.Context) {
			data := tushare.RecentLowList("20211028")
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data})
		})

		tushareGroup.POST("backFiveDayAvg", func(c *gin.Context) {
			data := tushare.BackFiveDayAvg("20211101")
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data})
		})

		tushareGroup.POST("tushareRaisingLimit", func(c *gin.Context) {
			data := tushare.RaisingLimitList(2)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data})
		})

		tushareGroup.GET("backFiveList", func(c *gin.Context) {
			tradeDate := c.Query("tradeDate")
			page, _ := strconv.ParseInt(c.Query("page"), 10, 64)
			pageSize, _ := strconv.ParseInt(c.Query("pageSize"), 10, 64)
			data := tushare.BackFiveList(page, pageSize, tradeDate)
			count := tushare.BackFiveCount(tradeDate)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data, "count": count})
		})

		tushareGroup.GET("avgLineAngleList", func(c *gin.Context) {
			tradeDate := c.Query("tradeDate")
			page, _ := strconv.ParseInt(c.Query("page"), 10, 64)
			pageSize, _ := strconv.ParseInt(c.Query("pageSize"), 10, 64)
			avgType, _ := strconv.ParseInt(c.Query("avgType"), 10, 64)
			data := tushare.AngleList(page, pageSize, tradeDate, avgType)
			count := tushare.AngleCount(tradeDate, avgType)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data, "count": count})
		})

		tushareGroup.GET("stockRiseFallCount", func(c *gin.Context) {
			riseCount, fallCount, count := model.StockRiseFall()
			c.JSON(200, gin.H{"code": 0, "message": "成功", "riseCount": riseCount, "fallCount": fallCount, "count": count})
		})

		tushareGroup.GET("angleNextDayRise", func(c *gin.Context) {
			page, _ := strconv.ParseInt(c.Query("page"), 10, 64)
			pageSize, _ := strconv.ParseInt(c.Query("pageSize"), 10, 64)
			avgType, _ := strconv.ParseInt(c.Query("avgType"), 10, 64)
			tradeDate := c.Query("tradeDate")

			data := tushare.AngleNextDayRise(page, pageSize, avgType, tradeDate)
			count := tushare.AngleNextDayRiseCount(avgType, tradeDate)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data, "count": count})
		})

		tushareGroup.GET("angleCount", func(c *gin.Context) {
			angle, _ := strconv.ParseInt(c.Query("angle"), 10, 64)
			avgType, _ := strconv.ParseInt(c.Query("avgType"), 10, 64)
			tradeDate := c.Query("tradeDate")

			count := tushare.AngleAvgLineCount(avgType, angle, tradeDate)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "count": count})
		})

		tushareGroup.GET("nowAngleList", func(c *gin.Context) {
			page, _ := strconv.ParseInt(c.Query("page"), 10, 64)
			pageSize, _ := strconv.ParseInt(c.Query("pageSize"), 10, 64)
			angle, _ := strconv.ParseFloat(c.Query("angle"), 64)
			imAmount, _ := strconv.ParseFloat(c.Query("imAmount"), 64)
			close, _ := strconv.ParseFloat(c.Query("close"), 64)

			data := tushare.NowAngleList(page, pageSize, angle, imAmount, close)
			count := tushare.NowAngleCount(angle, imAmount, close)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data, "count": count})
		})

		tushareGroup.GET("collect", func(c *gin.Context) {
			mapType := c.Query("type")
			tsCode := c.Query("tsCode")
			collect := c.Query("collect")
			ts := model.TushareStock{}
			if mapType == "collect" {
				ts.Collect = collect
			} else if mapType == "peakButton" {
				ts.PeakButton = collect
			} else if mapType == "peakCenter" {
				ts.PeakCenter = collect
			} else if mapType == "peakStable" {
				ts.PeakStable = collect
			}

			model.StockUpdate(tsCode, ts)
			c.JSON(200, gin.H{"code": 0, "message": "成功"})
		})

		tushareGroup.GET("getButtonClose", func(c *gin.Context) {
			diffValue, _ := strconv.ParseFloat(c.Query("diffValue"), 64)
			data := model.GetButtonClose(diffValue)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data})
		})

		tushareGroup.GET("getDaily", func(c *gin.Context) {
			tsCode := c.Query("tsCode")
			data := tushare.GetDaily(tsCode)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data})
		})

		tushareGroup.GET("elgVolList", func(c *gin.Context) {
			page, _ := strconv.ParseInt(c.Query("page"), 10, 64)
			pageSize, _ := strconv.ParseInt(c.Query("pageSize"), 10, 64)
			days, _ := strconv.ParseInt(c.Query("days"), 10, 64)
			tradeDate := c.Query("tradeDate")
			data := tushare.ElgList(page, pageSize, days, tradeDate)
			count := tushare.ElgCount(days, tradeDate)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data, "count": count})
		})

		tushareGroup.GET("minuteLine", func(c *gin.Context) {
			tsCode := c.Query("tsCode")
			line := new(serviceTushare.Line)
			data := line.MinuteLine(tsCode)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data})
		})

		tushareGroup.GET("elgVol", func(c *gin.Context) {
			tsCode := c.Query("tsCode")
			tradeDate := c.Query("tradeDate")
			limit, _ := strconv.ParseInt(c.Query("limit"), 10, 64)
			data := tushare.ElgVol(tsCode, tradeDate, limit)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data})
		})

		tushareGroup.GET("dailyMinList", func(c *gin.Context) {
			limit, _ := strconv.ParseInt(c.Query("limit"), 10, 64)
			data := tushare.DailyMinList(limit)
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data})
		})

		tushareGroup.GET("openDayList", func(c *gin.Context) {
			page, _ := strconv.ParseInt(c.Query("page"), 10, 64)
			pageSize, _ := strconv.ParseInt(c.Query("pageSize"), 10, 64)
			data := tushare.OpenDayList(page, pageSize)
			count := tushare.OpenDayCount()
			c.JSON(200, gin.H{"code": 0, "message": "成功", "data": data, "count": count})
		})
	}
}
